//
//  Database.m
//  DouBanDemo
//
//  Created by LiuFei on 14/11/10.
//  Copyright (c) 2014年 LiuFei. All rights reserved.
//

#import "Database.h"

#import "DB.h"
#import "Model.h"

@implementation Database

// 添加操作
+ (void)insertWithModel:(Model *)model table:(NSString *)table
{
    // 1 获得数据库指针
    sqlite3 *db = [DB shareDataBase];
    // 定义SQL语句
    NSString *sqlStr = [NSString stringWithFormat:@"insert into %@ (Id,Title,TData) values (%ld,'%@','%@')", table, model.Id, model.Title, model.Data];
    // 执行sql语句
    int result = sqlite3_exec(db, [sqlStr UTF8String], nil, nil, nil);
    // 判断是否执行成功
    if (result == SQLITE_OK) {
        NSLog(@"添加成功");
    } else {
        NSLog(@"添加失败");
    }
}

+ (void)deleteWithId:(NSInteger)bId table:(NSString *)table
{
    sqlite3 *db = [DB shareDataBase];
    NSString *sql = [NSString stringWithFormat:@"delete from %@ where activity_Id=%ld", table, bId];
    int result = sqlite3_exec(db, [sql UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    } else {
        NSLog(@"删除失败");
    }
}

+ (void)deleteAll:(NSString *)table
{
    sqlite3 *db = [DB shareDataBase];
    NSString *sql = [NSString stringWithFormat:@"delete from %@", table];
    int result = sqlite3_exec(db, [sql UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    } else {
        NSLog(@"删除失败");
    }
}



// 查询操作
+ (NSMutableArray *)selectAll:(NSString *)table
{
    sqlite3 *db = [DB shareDataBase];
    // 数据库指针的替身,直接对db操作有风险
    sqlite3_stmt *stmt = nil;
    
    NSString *sqlStr = [NSString stringWithFormat:@"select Id,Title,TData from %@", table];
    // 第三个参数：sql语句的长度，-1代表无限长;
    int result = sqlite3_prepare_v2(db, [sqlStr UTF8String], -1, &stmt, nil);
    NSMutableArray *dataArray = [NSMutableArray array];
    if (result == SQLITE_OK) {
        // 判断是否有下一条数据，并将其取出
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            // 取一条数据的属性，根据sql语句获得相对应属性
            int actId = sqlite3_column_int(stmt, 0);
            const unsigned char *cActUrl = sqlite3_column_text(stmt, 1);
            const unsigned char *cActDes = sqlite3_column_text(stmt, 2);
            
            NSString *actUrl = [NSString stringWithUTF8String:(const char *)cActUrl];
            NSString *actDes = [NSString stringWithUTF8String:(const char *)cActDes];
            
            Model *model = [Model modelWithId:actId title:actUrl Data:actDes];
            [dataArray addObject:model];
        }
    }
    sqlite3_finalize(stmt);
    return dataArray;
}

+ (Model *)selectWithId:(NSInteger)bId table:(NSString *)table
{
    sqlite3 *db = [DB shareDataBase];
    // 数据库指针的替身,直接对db操作有风险，
    sqlite3_stmt *stmt = nil;
    
    NSString *sqlStr = [NSString stringWithFormat:@"select activity_Id,activity_url,activity_des from %@ where activity_Id=%ld", table, bId];
    // 第三个参数：sql语句的长度，-1代表无限长;
    int result = sqlite3_prepare_v2(db, [sqlStr UTF8String], -1, &stmt, nil);
    
    if (result == SQLITE_OK) {
        // 判断是否有下一条数据，并将其取出
        if (sqlite3_step(stmt) == SQLITE_ROW) {
            // 取一条数据的属性，根据sql语句获得相对应属性
            int actId = sqlite3_column_int(stmt, 0);
            const unsigned char *cActUrl = sqlite3_column_text(stmt, 1);
            const unsigned char *cActDes = sqlite3_column_text(stmt, 2);
            
            NSString *title = [NSString stringWithUTF8String:(const char *)cActUrl];
            NSString *data = [NSString stringWithUTF8String:(const char *)cActDes];
//            NSData *data = [actDes dataUsingEncoding:NSUTF8StringEncoding];
            
            Model *model = [Model modelWithId:actId title:title Data:data];
            
            sqlite3_finalize(stmt);
            return model;
        }
    }
    sqlite3_finalize(stmt);
    return nil;
}



@end
